//
// Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
//
// GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
//  Author: Shawn.Tan
//  Date : 2025.10.28
//
//  History : Initial Version 2025.10.28
//
//
#include "GenStateTransitionAgent.h"

#include "GenRequest.h"
#include "Generator.h"
#include "Log.h"
#include "State.h"
#include "StateTransition.h"

using namespace std;

namespace Green {

  GenStateTransitionAgent::GenStateTransitionAgent()
    : GenAgent(), mpStateTransReq(nullptr)
  {
  }

  GenStateTransitionAgent::GenStateTransitionAgent(const GenStateTransitionAgent& rOther)
    : GenAgent(rOther), mpStateTransReq(rOther.mpStateTransReq)
  {
  }

  void GenStateTransitionAgent::SetGenRequest(GenRequest* genRequest)
  {
    mpStateTransReq = dynamic_cast<GenStateTransitionRequest*>(genRequest);
  }

  void GenStateTransitionAgent::HandleRequest()
  {
    LOG(notice) << "{GenStateTransitionAgent::HandleRequest} called" << endl;

    StateTransitionManagerRepository* state_trans_manager_repo = StateTransitionManagerRepository::Instance();
    StateTransitionManager* state_trans_manager = state_trans_manager_repo->GetStateTransitionManager(mpGenerator->ThreadId());
    const State* state = mpStateTransReq->TargetState();
    state_trans_manager->TransitionToState(*state, mpStateTransReq->StateTransitionType(), mpStateTransReq->OrderMode(), mpStateTransReq->StateElementTypeOrder());
  }

  void GenStateTransitionAgent::CleanUpRequest()
  {
    // If this is a one-time request, delete the State object
    auto one_time_state_trans_req = dynamic_cast<GenOneTimeStateTransitionRequest*>(mpStateTransReq);
    if (one_time_state_trans_req != nullptr) {
      delete mpStateTransReq->TargetState();
    }

    delete mpStateTransReq;
    mpStateTransReq = nullptr;
  }

}
